#include "EmergencyLights.h"
#include "GameplayState.h"
#include "Camera.h"

EmergencyLights::EmergencyLights()
{
	pgraphics = SGD::GraphicsManager::GetInstance();
	m_hImage = pgraphics->LoadTexture("resource/graphics/Emergency_Light_Flashing.png");

	m_rTextureSection = {6*32, 0, 7*32, 64 };

	m_nCell = 0;
	m_bForward = true;
}

EmergencyLights::~EmergencyLights()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hImage);
}

void EmergencyLights::Update(float dt)
{
	m_fTimer +=dt;
}

void EmergencyLights::Render()
{
	GameplayState* game = GameplayState::GetInstance();
	SGD::Point pos(m_ptPosition.x - game->GetCamX(), m_ptPosition.y - game->GetCamY());

	if (game->GetWorldTimer() > 15.f)
	{
		pgraphics->DrawTextureSection(m_hImage, pos, m_rTextureSection);
	}
	else if (game->GetWorldTimer() > 5.f)
	{
		if (m_fTimer > 0.05f)
		{
			m_fTimer = 0.f;

			if (m_bForward)
				m_nCell++;
			else
				m_nCell--;

			if (m_nCell > 5)
			{
				m_nCell = 4;
				m_bForward = false;
			}
			else if (m_nCell < 0)
			{
				m_nCell = 0;
				m_bForward = true;
			}
		}
		m_rTextureSection = {m_nCell*32.f,64,(m_nCell+1)*32.f,128};

		pgraphics->DrawTextureSection(m_hImage, pos, m_rTextureSection);
	}
	else if (game->GetWorldTimer() < 5.f)
	{
		if (m_fTimer > 0.05f)
		{
			m_fTimer = 0.f;

			if (m_bForward)
				m_nCell++;
			else
				m_nCell--;

			if (m_nCell > 4)
			{
				m_nCell = 3;
				m_bForward = false;
			}
			else if (m_nCell < 0)
			{
				m_nCell = 0;
				m_bForward = true;
			}
		}
		m_rTextureSection = { m_nCell*32.f, 0, (m_nCell + 1)*32.f, 64 };

		pgraphics->DrawTextureSection(m_hImage, pos, m_rTextureSection);
	}
}